﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Enterprise.Model.WorkFlow;
using Enterprise.Data.WorkFlow;
using Enterprise.Infrastructure;
using System.Data;

namespace Enterprise.Service.WorkFlow
{
    /// <summary>
    /// 流程业务服务
    /// </summary>
    public class WorkFlowInfoService
    {

        /// <summary>
        /// 用户缓存标记
        /// </summary>
        private static string cacheKey = "Enterprise-WorkFlow";

        /// <summary>
        /// 流程列表 返回所有流程记录
        /// </summary>
        /// <returns></returns>
        public List<WorkFlowInfoEntity> WorkFlowList()
        {
            if (CacheHelper.Contains(cacheKey))
            {
                return (List<WorkFlowInfoEntity>)CacheHelper.GetCache(cacheKey);
            }
            else
            {
                WorkFlowInfoData data = new WorkFlowInfoData();
                List<WorkFlowInfoEntity> list = data.WorkFlowList();
                CacheHelper.Add(cacheKey, list);
                return list;
            }
        }

        /// <summary>
        /// 返回流程记录信息
        /// </summary>
        /// <param name="Id">流程编码</param>
        /// <returns></returns>
        public WorkFlowInfoEntity WorkFlowDisp(int Id)
        {
            return WorkFlowList().Where(p => p.WFID == Id).FirstOrDefault();
        }

        private void ClearCache()
        {
            CacheHelper.RemoveCache(cacheKey);
        }

        /// <summary>
        /// 人员操作 添加、修改、删除
        /// </summary>
        /// <param name="entity">用户实体</param>
        public void WorkFlowExecute(WorkFlowInfoEntity entity)
        {
            string cmd = string.Empty;
            switch (entity.DB_Option_Action)
            {
                case "Insert":
                    cmd = "Insert into SYS_WORKFLOW(WFID,WFNAME,WFVERSION,WFTYPE,WFREMARK) values(SEQ_SYS_WORKFLOW.nextval,'" + entity.WFNAME + "'," + entity.WFVERSION + ",'" + entity.WFTYPE + "','" + entity.WFREMARK + "')";
                    break;
                case "Update":
                    cmd = "Update SYS_WORKFLOW set WFNAME='" + entity.WFNAME + "' , WFVERSION=" + entity.WFVERSION + " , WFTYPE='" + entity.WFTYPE + "' , WFREMARK='" + entity.WFREMARK + "' where WFID=" + entity.WFID;
                    break;
                case "Delete":
                    cmd = "Delete from SYS_WORKFLOW where WFID=" + entity.WFID;
                    break;
            }
            if (!string.IsNullOrEmpty(cmd))
            {
                WorkFlowExecuteBySql(cmd, 1);
            }
        }

        /// <summary>
        /// 人员操作方法
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="typeId">1:刷新缓存 0:不刷新缓存 </param>
        public void WorkFlowExecuteBySql(string cmd, int typeId)
        {
            WorkFlowInfoData data = new WorkFlowInfoData();
            data.WorkFlowExecute(cmd);
            if (typeId == 1)
            {
                CacheHelper.RemoveCache(cacheKey);
            }
        }

    }
}
